import {createRouter,createWebHashHistory} from 'vue-router'
import loginRouter from "./loginRouter";
import IndexRouter from "./IndexRouter";
const routes=[
    loginRouter,
    IndexRouter,
    //{ path: '/:pathMatch(.*)*', name: 'NotFound', component: NotFound },
]


const router = createRouter({
    // 4. 内部提供了 history 模式的实现。为了简单起见，我们在这里使用 hash 模式。
    history: createWebHashHistory(),
    routes, // `routes: routes` 的缩写
})
router.beforeEach((to,from,next)=>{
    console.log(to,from)
    document.title=to.meta.title;
    const isLogin=to.meta.isLogin;
    const isAdmin=to.meta.isAdmin;
    if (isLogin){
        const token=localStorage.getItem("token");
        const role=localStorage.getItem("role");
        console.log(typeof role)
        if (!token||!role){
            next('/login')
        }
        if (isAdmin){
            if (role!=='1'){
                next('/index/maintainOfCreditCard')
            }
        }
    }
    next()
})
export default router